import os
import sys
import csv
import time
import math
from redis import Redis, ConnectionPool


myRedis = {
    'host': '127.0.0.1',
    'port': 6379,
    'db': 2,
    'password': '123456'
}

class RedisClient:
    _pool = None
    _redis = None

    @classmethod
    def get_redis(cls):
        if cls._redis is None:
            if cls._pool is None:
                cls._pool = ConnectionPool(
                    host=myRedis['host'],
                    port=myRedis['port'],
                    db=myRedis['db'],
                    password=myRedis['password'],
                    decode_responses=True,  # 返回字符串而非字节
                    max_connections=50      # 可调大小
                )
            cls._redis = Redis(connection_pool=cls._pool)
        return cls._redis

def read_csv_to_list(file_path):
    """
    读取CSV文件内容到列表，每行作为一个元素
    :param file_path: CSV文件路径
    :return: 包含所有行的列表
    """
    data_list = []
    with open(file_path, mode='r', encoding='utf-8') as file:
        csv_reader = csv.reader(file)
        for row in csv_reader:
            if len(row)<4:
                continue
            data_list.append(row[-1])
    return data_list

def read_csv_to_generate_page_url(file_path):
    """
    读取CSV文件内容到列表，每行作为一个元素
    :param file_path: CSV文件路径
    :return: 包含所有行的列表
    """
    file_path_basename = os.path.basename(file_path).replace('.csv', '')
    data_list = []
    with open(file_path, mode='r', encoding='utf-8') as file:
        csv_reader = csv.reader(file)
        for row in csv_reader:
            if len(row)<3 or 'http' not in row[-1]:
                continue
            data_list.append(row[-1])
    
    page_num = math.ceil(len(data_list)/10)
    page_url_list = []
    for index in range(page_num):
        if '-view-' not in file_path_basename:
            subCategoryIndex = file_path_basename.replace('_', '/')
            page_url_list.append(f'https://home.meishichina.com/{subCategoryIndex}/page/{index+1}/')
        else:
            subCategoryIndex = file_path_basename
            page_url_list.append(f'https://home.meishichina.com/{subCategoryIndex}-page-{index+1}.html')

    return page_url_list

def read_csv_detail_to_list(file_path):
    """
    读取CSV文件内容到列表，每行作为一个元素
    :param file_path: CSV文件路径
    :return: 包含所有行的列表
    """
    data_list = []
    with open(file_path, mode='r', encoding='utf-8') as file:
        csv_reader = csv.reader(file)
        for row in csv_reader:
            if len(row)<2:
                continue
            data_list.append(row[0])
    return data_list

def csvToRedis(csvDIR=None):
    
    redis_cli = RedisClient.get_redis()

    if csvDIR is not None:
        csvDir = csvDIR
    else:
        csvDir = '/media/zhaosl/T7/菜谱/meishitianxia/CSV'
    csvFiles = [os.path.join(csvDir, csvFile) for csvFile in os.listdir(csvDir) if csvFile.endswith('.csv')]
    for index, csvFile in enumerate(csvFiles):
        category_index = os.path.basename(csvFile).replace('.csv','')
        data_list = read_csv_to_generate_page_url(csvFile)
        for dish_link in data_list:
            redis_cli.sadd(f'caipu:Meishitianxia:{category_index}', dish_link)
        print(f'{index} : {category_index} DONE')
    print(f'添加完毕...... 共 {len(csvFiles)} 个键值')

def csvDetailToRedis(csvDirDetail=None):
    
    redis_cli = RedisClient.get_redis()

    if csvDirDetail is not None:
        csvDir = csvDirDetail
    else:
        csvDir = '/media/zhaosl/T7/菜谱/meishitianxia/CSVDetail'
    csvFiles = [os.path.join(csvDir, csvFile) for csvFile in os.listdir(csvDir) if csvFile.endswith('.csv')]
    for index, csvFile in enumerate(csvFiles):
        category_index = os.path.basename(csvFile).replace('.csv','')
        data_list = read_csv_detail_to_list(csvFile)

        for dish_link in data_list:
            redis_cli.sadd(f'caipu:Meishitianxia:dish_link_detail:{category_index}', dish_link)
        print(f'{index} : {category_index} DONE')
    print(f'添加完毕...... 共 {len(csvFiles)} 个键值')

def delete_redis_dish_link_details(dele_patten='caipu:Meishitianxia:dish_link_detail:*'):
    redis_cli = RedisClient.get_redis()
    
    # 使用scan_iter来高效地遍历所有匹配的键
    deleted_count = 0
    for key in redis_cli.scan_iter(match=dele_patten):
        redis_cli.delete(key)
        deleted_count += 1
    
    print(f"Deleted {deleted_count} keys with prefix {dele_patten}")
    print('删除完毕......')
    return deleted_count

def get_dish_link_keys(key_pattern="caipu:Meishitianxia:dish_link_detail:*"):
    """
    获取所有以 'caipu:Meishitianxia:dish_link_detail:' 为前缀的键名
    
    Returns:
        list: 匹配的键名列表
    """
    redis_cli = RedisClient.get_redis()
    
    # 使用scan_iter高效遍历键名
    key_list= list(redis_cli.scan_iter(match=key_pattern))
    
    if key_list:
        for item in key_list:
            print(item)
    else:
        print(f'没有查询到以 {key_pattern} 为前缀的键值集合')
    print(f'查询完毕...... 共 {len(key_list)} 个键值')

def main():
    
    if 'detail' in sys.argv[1].lower():
        key_patten = 'caipu:Meishitianxia:dish_link_detail:*'
        get_dish_link_keys(key_patten)
        time.sleep(2)
        delete_redis_dish_link_details(key_patten)
        time.sleep(2)
        get_dish_link_keys(key_patten)
        time.sleep(2)
        csvDetailToRedis(sys.argv[2])
        time.sleep(2)
        get_dish_link_keys(key_patten)
    else:
        key_patten = 'caipu:Meishitianxia:*'
        get_dish_link_keys(key_patten)
        time.sleep(2)
        delete_redis_dish_link_details(key_patten)
        time.sleep(2)
        get_dish_link_keys(key_patten)
        time.sleep(2)
        csvToRedis(sys.argv[2])
        time.sleep(2)
        get_dish_link_keys(key_patten)



    pass

if __name__ == "__main__":
    main()
    #python csvToRedis.py csv ./CSV
    #python csvToRedis.py csvDetail ./CSVDetail'